# -*- coding=utf-8 -*-
# Created Time: 2016年09月12日 星期一 11时38分12秒
# File Name: record.py

"""
操作记录
"""

from utils.routes      import route
from utils             import mongo
from utils.errors      import err_param
from utils.pages       import pages_count
from utils.lib_re      import regex_char_trans

from service.base      import AuthHandler
from loog.mylog         import flogger as logger
from configure.config  import DB_NAME, COLL_REC, COLL_CLASSLOG,\
                              COLL_CHANNELLOG

import json, tornado, pymongo


@tornado.gen.engine
def keep(data, callback=None):
    """ 记录 其他记录 """

    try:
        yield tornado.gen.Task(mongo.insert, DB_NAME, COLL_REC, data)
    except Exception, e:
        logger.exception(e)
        callback(False)

    callback(True)


@route(r'/record/other/list', name='record_other_list')
class RecordOtherListHandler(AuthHandler):
    """ 操作记录 - 其他记录 """

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        try:
            udict    = json.loads(self.request.body)

            keyword  = udict.get('keyword')
            page     = udict.get('page',1)
            limit    = udict.get('page_size', 30)
        except Exception, e:
            logger.exception(e)
            self.write(err_param)
            self.finish()
            return

        query  = {}
        skip   = (page-1)*limit
        sort   = [['created', pymongo.DESCENDING]]
        fields = {'keyword':0}
        if keyword:
            keyword = regex_char_trans(keyword)
            query.update({'keyword': {'$regex':keyword}})

        try:
            seeds = yield tornado.gen.Task(
                                        mongo.find_all,
                                        DB_NAME,
                                        COLL_REC,
                                        query,
                                        skip=skip,
                                        limit=limit,
                                        sort=sort,
                                        fields=fields)
            count = yield tornado.gen.Task(
                                        mongo.count,
                                        DB_NAME,
                                        COLL_REC,
                                        sort,
                                        query)
        except Exception, e:
            logger.exception(e)
            self.write({'code':500})
            self.finish()
            return

        pages = pages_count(count, limit)
        self.write({
            'code'     :200,
            'page'     :page,
            'page_size':limit,
            'pages'    :pages,
            'data'     :seeds
        })
        self.finish()


@route(r'/record/push/list', name='record_push_list')
class RecordPushListHandler(AuthHandler):
    """ 操作记录 - 其他记录 """

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        try:
            udict    = json.loads(self.request.body)

            keyword  = udict.get('keyword')
            page     = udict.get('page',1)
            limit    = udict.get('page_size', 30)
        except Exception, e:
            logger.exception(e)
            self.write(err_param)
            self.finish()
            return

        query  = {}
        skip   = (page-1)*limit
        sort   = [['created', pymongo.DESCENDING]]
        fields = {'keyword':0}
        if keyword:
            keyword = regex_char_trans(keyword)
            query.update({'keyword': {'$regex':keyword}})

        try:
            seeds = yield tornado.gen.Task(
                                        mongo.find_all,
                                        DB_NAME,
                                        COLL_CLASSLOG,
                                        query,
                                        skip=skip,
                                        limit=limit,
                                        sort=sort,
                                        fields=fields)
            count = yield tornado.gen.Task(
                                        mongo.count,
                                        DB_NAME,
                                        COLL_CLASSLOG,
                                        sort,
                                        query)
        except Exception, e:
            logger.exception(e)
            self.write({'code':500})
            self.finish()
            return

        pages = pages_count(count, limit)
        self.write({
            'code'     :200,
            'page'     :page,
            'page_size':limit,
            'pages'    :pages,
            'data'     :seeds
        })
        self.finish()


@route(r'/record/pub/list', name='record_pub_list')
class RecordPubListHandler(AuthHandler):
    """ 操作记录 - 其他记录 """

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        try:
            udict    = json.loads(self.request.body)

            keyword  = udict.get('keyword')
            page     = udict.get('page',1)
            limit    = udict.get('page_size', 30)
        except Exception, e:
            logger.exception(e)
            self.write(err_param)
            self.finish()
            return

        query  = {}
        skip   = (page-1)*limit
        sort   = [['created', pymongo.DESCENDING]]
        fields = {'keyword':0}
        if keyword:
            keyword = regex_char_trans(keyword)
            query.update({'keyword': {'$regex':keyword}})

        try:
            seeds = yield tornado.gen.Task(
                                        mongo.find_all,
                                        DB_NAME,
                                        COLL_CHANNELLOG,
                                        query,
                                        skip=skip,
                                        limit=limit,
                                        sort=sort,
                                        fields=fields)
            count = yield tornado.gen.Task(
                                        mongo.count,
                                        DB_NAME,
                                        COLL_CHANNELLOG,
                                        sort,
                                        query)
        except Exception, e:
            logger.exception(e)
            self.write({'code':500})
            self.finish()
            return

        pages = pages_count(count, limit)
        self.write({
            'code'     :200,
            'page'     :page,
            'page_size':limit,
            'pages'    :pages,
            'data'     :seeds
        })
        self.finish()



